home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ViewTester / Sources / Part.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  14.2 KB  |  522 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                Part.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #ifndef PART_H
  11. #include "Part.h"
  12. #endif
  13.  
  14. #ifndef FRAME_H
  15. #include "Frame.h"
  16. #endif
  17.  
  18. #ifndef DIALOG_H
  19. #include "Dialog.h"
  20. #endif
  21.  
  22. #ifndef DEFINES_K
  23. #include "Defines.k"
  24. #endif
  25.  
  26. #ifndef BINDING_K
  27. #include "Binding.k"
  28. #endif
  29.  
  30. // ----- Framework Includes -----
  31.  
  32. #ifndef FWUTIL_H
  33. #include "FWUtil.h"
  34. #endif
  35.  
  36. #ifndef FWITERS_H
  37. #include "FWIters.h"
  38. #endif
  39.  
  40. #ifndef FWCFMRES_H
  41. #include "FWCFMRes.h"
  42. #endif
  43.  
  44. #ifndef FWABOUT_H
  45. #include "FWAbout.h"
  46. #endif
  47.  
  48. #ifndef FWMENUS_K
  49. #include "FWMenus.k"
  50. #endif
  51.  
  52. #ifndef FWMNUBAR_H
  53. #include "FWMnubar.h"
  54. #endif
  55.  
  56. #ifndef SLMIXOS_H
  57. #include "SLMixOS.h"
  58. #endif
  59.  
  60. // ----- PPob View Support -----
  61.  
  62. #if FW_PPOB_VIEWS
  63. #include "FWPPobRd.h"
  64. #endif
  65.  
  66. // ----- MacApp View Support -----
  67.  
  68. #if FW_MACAPP_VIEWS
  69. #include "FWMARead.h"
  70. #endif
  71.  
  72. //========================================================================================
  73. //    Runtime informations
  74. //========================================================================================
  75.  
  76. #ifdef FW_BUILD_MAC
  77. #pragma segment viewtester
  78. #endif
  79.  
  80. //========================================================================================
  81. //    CLASS CViewTesterPart
  82. //========================================================================================
  83.  
  84.  
  85. FW_DEFINE_AUTO(CViewTesterPart)
  86.     
  87. //----------------------------------------------------------------------------------------
  88. //     CViewTesterPart::CViewTesterPart
  89. //----------------------------------------------------------------------------------------
  90.  
  91. CViewTesterPart::CViewTesterPart(ODPart* odPart):
  92.     FW_CPart(odPart, FW_gInstance, kPartInfoID),
  93.     fViewTestFrame(0),
  94.     fViewID(-1),
  95.     fUsingMacApp(true)
  96. {
  97. }
  98.  
  99. //----------------------------------------------------------------------------------------
  100. //     CViewTesterPart::~CViewTesterPart
  101. //----------------------------------------------------------------------------------------
  102.  
  103. CViewTesterPart::~CViewTesterPart()
  104. {
  105. }
  106.  
  107. //----------------------------------------------------------------------------------------
  108. //     CViewTesterPart::Initialize
  109. //----------------------------------------------------------------------------------------
  110.  
  111. void CViewTesterPart::Initialize(Environment* ev, ODStorageUnit* storageUnit, FW_Boolean fromStorage)
  112. {
  113.     FW_CPart::Initialize(ev, storageUnit, fromStorage);
  114.     
  115.     fMainPresentation = RegisterPresentation(ev, kMainPresentation, TRUE, kMainViewID, kMainViewID);
  116.     fDialogPresentation = RegisterPresentation(ev, kDialogPresentation, FALSE, kDialogID, kDialogID);
  117.     fTestPresentation = RegisterPresentation(ev, kTestPresentation, FALSE);
  118.  
  119. #if FW_MACAPP_VIEWS
  120.     // Register the standard MacApp classes
  121.     FW_CMacAppReader::RegisterAllMacAppClasses();
  122. #endif
  123.  
  124. #if FW_PPOB_VIEWS
  125.     // Register the standard PP classes
  126.     FW_CPPobReader::RegisterAllPPClasses();
  127. #endif    
  128.  
  129.     // Make sure that classes created from ODFRC resources won't be dead-stripped!
  130.     FW_DO_NOT_DEAD_STRIP(FW_CGrowBox);
  131.     FW_DO_NOT_DEAD_STRIP(FW_CScrollBarScroller);
  132.     FW_DO_NOT_DEAD_STRIP(FW_CGroupBox);
  133.     FW_DO_NOT_DEAD_STRIP(FW_CScrollBar);
  134.     FW_DO_NOT_DEAD_STRIP(FW_CEditView);
  135.     FW_DO_NOT_DEAD_STRIP(FW_CListBox);
  136.     FW_DO_NOT_DEAD_STRIP(FW_CButton);
  137.     FW_DO_NOT_DEAD_STRIP(FW_CRadioCluster);
  138.     FW_DO_NOT_DEAD_STRIP(FW_CPopupMenu);
  139.     FW_DO_NOT_DEAD_STRIP(FW_CStaticText);
  140.     FW_DO_NOT_DEAD_STRIP(FW_CPictSView);
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. //     CViewTesterPart::NewFrame
  145. //----------------------------------------------------------------------------------------
  146.  
  147. FW_CFrame* CViewTesterPart::NewFrame(Environment* ev, 
  148.                                   ODFrame* odFrame, 
  149.                                   FW_CPresentation* presentation, 
  150.                                   FW_Boolean fromStorage)
  151. {
  152. FW_UNUSED(fromStorage);
  153.     FW_CFrame* newFrame = NULL;
  154.     
  155.     if (presentation == fMainPresentation) 
  156.         newFrame = FW_NEW(FW_CFrame, (ev, odFrame, presentation, this));
  157.     else if (presentation == fDialogPresentation) 
  158.         newFrame = FW_NEW(CDialogFrame, (ev, odFrame, presentation, this));
  159.     else if (presentation == fTestPresentation) 
  160.     {
  161.         if(fViewTestFrame == 0)
  162.         {
  163.             newFrame = FW_NEW(CViewTesterFrame, (ev, odFrame, presentation, this));
  164.             fViewTestFrame = FW_DYNAMIC_CAST(CViewTesterFrame, newFrame);
  165.         }
  166.         else
  167.             FW_DEBUG_MESSAGE("This parts allows only 1 test frame");
  168.     }
  169.  
  170.     return newFrame;
  171. }
  172.  
  173. //----------------------------------------------------------------------------------------
  174. //     CViewTesterPart::NewPartContent
  175. //----------------------------------------------------------------------------------------
  176.  
  177. FW_CContent* CViewTesterPart::NewPartContent(Environment* ev)
  178. {
  179.     return NULL;
  180. }
  181.  
  182. //----------------------------------------------------------------------------------------
  183. //    CViewTesterPart::DoMenu
  184. //----------------------------------------------------------------------------------------
  185.  
  186. FW_Handled CViewTesterPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  187. {
  188.     FW_Handled handled = FW_kHandled;
  189.     FW_PlatformError error;
  190.         
  191.     ODCommandID commandID = theMenuEvent.GetCommandID(ev);
  192.     switch (commandID)
  193.     {
  194.         case cReloadMAViews:    
  195.             FW_ASSERT(fViewTestFrame);
  196.             fUsingMacApp = true;
  197.             error = FW_CMacAppReader::ReloadViews(ev, 
  198.                                                 fViewID, 
  199.                                                 fViewTestFrame, 
  200.                                                 0, 
  201.                                                 FW_CMacAppReader::kCurrentResFile,
  202.                                                 HasWarningsOn(ev));
  203.             CheckForError(ev, error);
  204.             break;
  205.         case cReloadPPobViews:    
  206.             FW_ASSERT(fViewTestFrame);
  207.             fUsingMacApp = false;
  208.             if (theMenuEvent.IsExtendModifier(ev))
  209.                 FW_CPPobReader::UseClassAlias(true);
  210.  
  211.             error = FW_CPPobReader::ReloadViews(ev, 
  212.                                                 fViewID, 
  213.                                                 fViewTestFrame, 
  214.                                                 0, 
  215.                                                 FW_CPPobReader::kCurrentResFile,
  216.                                                 HasWarningsOn(ev));
  217.             FW_CPPobReader::UseClassAlias(false);
  218.             CheckForError(ev, error);
  219.             break;
  220.  
  221.         case cLoadNewMAViews:
  222.             fUsingMacApp = true;
  223.             OpenDialog(ev);
  224.             break;
  225.         case cLoadNewPPobViews:
  226.             fUsingMacApp = false;
  227.             OpenDialog(ev);
  228.             break;
  229.  
  230.         case cLoadNewMAResFile:    
  231.             {
  232.                 // Pick a new resource file first.
  233.                 FW_PResourceFile* resFile = FW_CMacAppReader::GetResFile(ev, 
  234.                                                     FW_CMacAppReader::kNewResFile);
  235.                 if (resFile)
  236.                 {
  237.                     delete resFile;
  238.                     fUsingMacApp = true;
  239.                 
  240.                     OpenDialog(ev);    // Pick a resource id in the dialog
  241.                 }
  242.             }
  243.             break;
  244.         case cLoadNewPPobResFile:    
  245.             {
  246.                 // Pick a new resource file first.
  247.                 FW_PResourceFile* resFile = FW_CPPobReader::GetResFile(ev, 
  248.                                                     FW_CPPobReader::kNewResFile);
  249.                 if (resFile)
  250.                 {
  251.                     delete resFile;
  252.                     fUsingMacApp = false;
  253.  
  254.                     OpenDialog(ev); // Pick a resource id in the dialog
  255.                 }
  256.             }
  257.             break;
  258.  
  259.         default:
  260.             handled = FW_kNotHandled;
  261.     }
  262.             
  263.     return handled;
  264. }
  265.  
  266. //----------------------------------------------------------------------------------------
  267. //    CViewTesterPart::DoAbout
  268. //----------------------------------------------------------------------------------------
  269.  
  270. FW_Handled CViewTesterPart::DoAbout(Environment* ev)
  271. {
  272.     ::FW_About(ev, this, kAbout);
  273.     
  274.     return FW_kHandled;
  275. }
  276.  
  277. //----------------------------------------------------------------------------------------
  278. //    CViewTesterPart::DoAdjustMenus
  279. //----------------------------------------------------------------------------------------
  280.  
  281. FW_Handled CViewTesterPart::DoAdjustMenus(Environment* ev, 
  282.                                         FW_CMenuBar* menuBar, 
  283.                                         FW_Boolean hasMenuFocus, 
  284.                                         FW_Boolean isRoot)
  285. {
  286.     // ----- Edit Menu -----
  287.     if (hasMenuFocus)
  288.     {
  289. #if FW_MACAPP_VIEWS
  290.         {
  291.             FW_Boolean hasResFile = FW_CMacAppReader::HasResFile();
  292.             FW_Boolean canReload = hasResFile && (fViewTestFrame != 0);
  293.             menuBar->EnableCommand(ev, cReloadMAViews, canReload); 
  294.             menuBar->EnableCommand(ev, cLoadNewMAViews, hasResFile); 
  295.             menuBar->EnableCommand(ev, cLoadNewMAResFile, true); 
  296.         }
  297. #endif
  298. #if FW_PPOB_VIEWS
  299.         {
  300.             FW_Boolean hasResFile = FW_CPPobReader::HasResFile();
  301.             FW_Boolean canReload = hasResFile && (fViewTestFrame != 0);
  302.             menuBar->EnableCommand(ev, cReloadPPobViews, canReload); 
  303.             menuBar->EnableCommand(ev, cLoadNewPPobViews, hasResFile); 
  304.             menuBar->EnableCommand(ev, cLoadNewPPobResFile, true); 
  305.         }
  306. #endif
  307.     }
  308.     
  309.     return FW_kNotHandled;
  310. }
  311.  
  312. //----------------------------------------------------------------------------------------
  313. //    CViewTesterPart::OpenDialog
  314. //----------------------------------------------------------------------------------------
  315.  
  316. void CViewTesterPart::OpenDialog(Environment* ev)
  317. {
  318.     // Prepare dialog attributes
  319.  
  320.     FW_CPoint    position(FW_kZeroPoint);
  321.     FW_CPoint    size(FW_IntToFixed(330), FW_IntToFixed(160));
  322.     FW_WindowStyle style = FW_kStandardDialogPosition | FW_kHasCaption;
  323.  
  324.     FW_CString fileName;
  325.     if (fUsingMacApp)
  326.         FW_CMacAppReader::GetResFileName(ev, fileName);
  327.     else
  328.         FW_CPPobReader::GetResFileName(ev, fileName);
  329.  
  330.     FW_CString title = "Resources of ";
  331.     title += fileName;
  332.     
  333.     // HACK: in order to create the popup menus containing the list of MacApp or PPob
  334.     // resources we need to open the proper resource file before creating the dialog 
  335.  
  336.     FW_PResourceFile* resFile = 0;
  337.     if (fUsingMacApp)
  338.         resFile = FW_CMacAppReader::GetResFile(ev, FW_CMacAppReader::kCurrentResFile);
  339.     else
  340.         resFile = FW_CPPobReader::GetResFile(ev, FW_CPPobReader::kCurrentResFile);
  341.  
  342.     // ----- Create new dialog window  -----
  343.         
  344.     CDialogFrame* dialog = (CDialogFrame*) FW_CDialogFrame::NewModalDialog(ev, 
  345.                                                                 this,    
  346.                                                                 fDialogPresentation, 
  347.                                                                 size,         
  348.                                                                 position,     
  349.                                                                 style,        
  350.                                                                 title);    
  351.     
  352.     delete resFile;
  353.  
  354.     if (dialog != NULL)
  355.     {                        
  356.         dialog->Initialize(ev);        
  357.         dialog->GetWindow(ev)->Show(ev);
  358.     }
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    CViewTesterPart::CheckForError
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void    CViewTesterPart::CheckForError(Environment* ev, FW_PlatformError error)
  366. {
  367.     FW_CString fileName;
  368.     if (fUsingMacApp)
  369.         FW_CMacAppReader::GetResFileName(ev, fileName);
  370.     else
  371.         FW_CPPobReader::GetResFileName(ev, fileName);
  372.  
  373.     FW_CString idstr;
  374.     idstr.ReplaceAllAsUnsignedDecimalInteger(fViewID);
  375.     
  376.     // Bring the test frame's window to the front and update its title
  377.     FW_CString winTitle;
  378.     winTitle = fileName;
  379.     winTitle += " (view id ";
  380.     winTitle += idstr;
  381.     winTitle += ")";
  382.     FW_CWindow* testWindow = fViewTestFrame->GetWindow(ev);
  383.     testWindow->SetWindowTitle(ev, winTitle);
  384.     testWindow->Select(ev);
  385.  
  386.  
  387.     // Update the error log
  388.     FW_CString errorStr = "";
  389.     
  390.      if (error != FW_xNoError)
  391.      {
  392.          FW_Beep();
  393.          
  394.         errorStr += "Error reading ";
  395.         if (fUsingMacApp)
  396.             errorStr += "MacApp 'View' resource ID ";
  397.         else
  398.             errorStr += "PowerPlant 'PPob' resource ID ";
  399.         errorStr += idstr;
  400. //        errorStr += " in ";
  401. //        errorStr += fileName;
  402.  
  403.         switch(error)
  404.         {
  405.             case FW_xResourceNotFound:
  406.             case FW_xResourceNotLoaded:
  407.                 errorStr += ": resource not found!";
  408.                 break;
  409.  
  410.             case FW_xReadableStream:
  411.                 errorStr += ": found invalid data or couldn't skip unknown view!";
  412.                 break;
  413.                 
  414.             case FW_xWrongViewResourceVersion:
  415.                 errorStr += ": wrong view resource version!";
  416.                 break;
  417.                 
  418.             case FW_xUnknownError:
  419.                 errorStr += ": unknown error! (Please report to the ODF-Interest list)";
  420.                 break;
  421.             
  422.             default:
  423.                 FW_CString errorStr;
  424.                 errorStr.ReplaceAllAsSignedDecimalInteger(error);
  425.                 errorStr += ": ODF error ";
  426.                 errorStr += errorStr;
  427.                 errorStr += " (see definitions in FWErrors.h)";
  428.                 break;
  429.         }        
  430.     }    
  431.     else if (fUsingMacApp == false && FW_CPPobReader::FoundViewWithAlias() == true)
  432.     {
  433.          FW_Beep();
  434.         errorStr += "Some pink (unregistered) views are simple subclasses of registered views. Hold the Shift key down and do 'Reload Views' to use the known views";
  435.     }
  436.     
  437.     FW_CPresentationFrameIterator ite(ev, fMainPresentation);
  438.     for (FW_CFrame* frame = ite.First(ev); ite.IsNotComplete(ev); frame = ite.Next(ev))
  439.     {
  440.         FW_CView* view =  frame->FindViewByID(ev, 1);
  441.         FW_CEditView* viewTEd = FW_DYNAMIC_CAST(FW_CEditView, view);
  442.         FW_ASSERT(viewTEd);
  443.         viewTEd->SetText(ev, errorStr);
  444.     }
  445. }
  446.  
  447. //----------------------------------------------------------------------------------------
  448. //    CViewTesterPart::ChangeViews
  449. //----------------------------------------------------------------------------------------
  450.  
  451. void    CViewTesterPart::ChangeViews(Environment* ev, ODID viewID)
  452. {
  453.     fViewID = viewID;
  454.     
  455.     if (fViewTestFrame == 0)
  456.     {
  457.         // Create the test frame in a new window.
  458.         
  459.         FW_CPoint size(FW_IntToFixed(200), FW_IntToFixed(100));
  460.         FW_CPoint position(FW_IntToFixed(3), FW_IntToFixed(3));
  461.  
  462.         FW_CWindow* testWindow = new FW_CWindow(ev, this, 
  463.                                                 NULL,             // no parent frame
  464.                                                 FALSE,             // not persistent
  465.                                                 FW_CPart::fgViewAsFrameToken,
  466.                                                 fTestPresentation,
  467.                                                 "TestFrame",    // updated after loading views
  468.                                                 size,            // updated after loading views
  469.                                                 position,        // updated after loading views
  470.                                                 FW_kDocumentWindow);
  471.     
  472.         FW_ASSERT(fViewTestFrame);
  473.         
  474.         // Showing the window will call FacetAdded and CViewTesterFrame::CreateSubView
  475.         testWindow->Show(ev);
  476.     }
  477.     else
  478.     {
  479.         FW_PlatformError error;
  480.  
  481.         if (fUsingMacApp)
  482.             error = FW_CMacAppReader::ReloadViews(ev, 
  483.                                                     viewID, 
  484.                                                     fViewTestFrame, 
  485.                                                     0, 
  486.                                                     FW_CMacAppReader::kCurrentResFile,
  487.                                                     HasWarningsOn(ev));
  488.         else
  489.             error = FW_CPPobReader::ReloadViews(ev, 
  490.                                                 viewID, 
  491.                                                 fViewTestFrame,
  492.                                                 0, 
  493.                                                 FW_CPPobReader::kCurrentResFile,
  494.                                                 HasWarningsOn(ev));
  495.         CheckForError(ev, error);
  496.     }
  497. }
  498.  
  499.  
  500. //----------------------------------------------------------------------------------------
  501. //    CViewTesterPart::HasWarningsOn
  502. //----------------------------------------------------------------------------------------
  503.  
  504. FW_Boolean    CViewTesterPart::HasWarningsOn(Environment* ev)
  505. {
  506.     // This is not very clean.. we read the warnings check-box state from the 1st frame.
  507.     // (I was lazy and didn't want to create a FW_CFrame class for fMainPresentation!)
  508.  
  509.     FW_CPresentationFrameIterator ite(ev, fMainPresentation);
  510.     FW_CFrame* frame = ite.First(ev);
  511.     
  512.     if (frame)
  513.     {
  514.         FW_CView* view =  frame->FindViewByID(ev, 2);
  515.         FW_CButton* button = FW_DYNAMIC_CAST(FW_CButton, view);
  516.         return (button->GetState(ev) == false);
  517.     }
  518.     else
  519.         return true;
  520. }
  521.  
  522.